package com.lijing.ThreadedBinaryTree;

import java.security.PublicKey;

/**
 * @Description 前序遍历线索化
 * @title: preOrderThreadedBinaryTree
 * @Author LiJing
 * @Date: 2021/2/193:03 下午
 * @Version 1.0
 */
public class PreOrderThreadedBinaryTree {
    private HeroNode root;
    private HeroNode pre;

    public PreOrderThreadedBinaryTree() {
    }
    public PreOrderThreadedBinaryTree(HeroNode root) {
        this.root = root;
    }

    public HeroNode getRoot() {
        return root;
    }
    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void preOrderThreadedNodes(){
        this.preOrderThreadedNodes(root);
    }
    /**
     * 前序遍历线索化，先当前节点，然后←然后→
     * @param node 待线索化的节点
     */
    public void preOrderThreadedNodes(HeroNode node){
        if (node == null){
            return;
        }
        /*
         * @Date: 2021/2/19 3:11 下午
         * Step 1: 线索化当前节点
         */
        if (node.getLeft() == null){
            node.setLeft(pre);
            node.setLeftType(1);
        }
        if (pre != null && pre.getRight() == null){
            pre.setRight(node);
            pre.setRightType(1);
        }
        pre = node;
        /*
         * @Date: 2021/2/19 3:12 下午
         * Step 2: 线索化左子树
         */
        if (node.getLeftType() == 0){
            preOrderThreadedNodes(node.getLeft());
        }

        /*
         * @Date: 2021/2/19 3:12 下午
         * Step 3: 线索化右子树
         */
        if (node.getRightType() == 0){
            preOrderThreadedNodes(node.getRight());
        }

    }

    public void preOrderThreadedList(){
        this.preOrderThreadedList(root);
    }
    /**
     * 前序线索化遍历
     */
    public void preOrderThreadedList(HeroNode node){

        /*
         * @Date: 2021/2/19 4:52 下午
         * Step 1: 打印当前节点
         */
        System.out.println(node);
        /*
         * @Date: 2021/2/19 4:52 下午
         * Step 2: 打印左节点
         */
        if (node.getLeft() != null && node.getLeftType() == 0){
            preOrderThreadedList(node.getLeft());
        }
        /*
         * @Date: 2021/2/19 4:54 下午
         * Step 3: 打印右子树
         */
        if (node.getRight() != null && node.getRightType() == 0){
            preOrderThreadedList(node.getRight());
        }
    }
    public void preOrderThreadedList1(){
    }
}

